#!groovy
@Library('jenkins-pipeline-lib') _

pipeline
{
	agent any
	stages
	{
		stage('Clean')
		{
			when
			{
				expression
				{
					/*
					* If the previous build suceeeded (unstable means test failed but build passed)
					* then we continue on in CI mode. If the previous build failed we want to
					* start with a clean environment. This is done to reduce manual user interation.
					*/
					return !(didLastBuildSucceed())
				}
			}
			steps
			{
				echo('Previous build failed: Running a clean build.')
				sh 'make distclean'
			}
		}
		stage('Native compilation')
		{
			steps
			{
				sh 'make'
			}
		}
		stage('Tests')
		{
			steps
			{
				sh 'make test'
			}
		}
		stage('Cross compile for AVR')
		{
			steps
			{
				sh 'make CROSS=avr:arduino_mega BUILDRESULTS=buildresults/avr'
			}
			post
			{
				always
				{
					recordIssues(
						healthy: 5,
						unhealthy: 10,
						aggregatingResults: true,
						referenceJobName: 'ea-nightly/arduino-logger/master',
						sourceDirectory: 'buildresults/avr/',
						filters: [
							excludeFile('subprojects/*')
						],
						qualityGates: [
							// 3 new issue: unstable
							[threshold: 3, type: 'DELTA', unstable: true],
							// 5 new issues: failed build
							[threshold: 5, type: 'DELTA', unstable: false],
							// 10 total issues: unstable
							[threshold: 10, type: 'TOTAL', unstable: true],
							// 20 total issues: fail
							[threshold: 20, type: 'TOTAL', unstable: false]
						],
						tools: [
							gcc(id: 'gcc-avr', name: 'gcc-avr'),
						]
					)
				}
			}
		}
		stage('CppCheck')
		{
			steps
			{
				sh 'make cppcheck-xml'
			}
			post
			{
				always
				{
					recordIssues(
						healthy: 5,
						unhealthy: 10,
						aggregatingResults: true,
						referenceJobName: 'ea-nightly/arduino-logger/master',
						filters: [
							excludeFile('subprojects/*')
						],
						qualityGates: [
							// 3 new issue: unstable
							[threshold: 3, type: 'DELTA', unstable: true],
							// 5 new issues: failed build
							[threshold: 5, type: 'DELTA', unstable: false],
							// 10 total issues: unstable
							[threshold: 10, type: 'TOTAL', unstable: true],
							// 20 total issues: fail
							[threshold: 20, type: 'TOTAL', unstable: false]
						],
						tools: [
							cppCheck(
								pattern: 'buildresults/**/cppcheck.xml',
							),
						]
					)
				}
			}
		}
	}
	post
	{
		always
		{
			// Scan for open tasks, warnings, issues, etc.
			recordIssues(
				healthy: 5,
				unhealthy: 10,
				aggregatingResults: true,
				referenceJobName: 'ea-nightly/arduino-logger/master',
				filters: [
					excludeFile('subprojects/*')
				],
				qualityGates: [
					// 3 new issue: unstable
					[threshold: 3, type: 'DELTA', unstable: true],
					// 5 new issues: failed build
					[threshold: 5, type: 'DELTA', unstable: false],
					// 10 total issues: unstable
					[threshold: 10, type: 'TOTAL', unstable: true],
					// 20 total issues: fail
					[threshold: 20, type: 'TOTAL', unstable: false]
				],
				tools: [
					taskScanner(
						excludePattern: 'buildresults/**, subprojects/**, build/**, extras/**, test/catch/**',
						includePattern: '**/*.c, **/*.cpp, **/*.h, **/*.hpp, **/*.sh, **/*.build, **/*.ino',
						normalTags: 'TODO, to do, WIP',
						highTags: 'FIXME, FIX',
						ignoreCase: true,
					),
				]
			)
		}
	}
}
